import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const routes = [
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/home'),
    meta: {
      title: '机构风险信息列表'
    }
  },
  {
    path: '/info',
    name: 'info',
    component: () => import('@/views/info'),
    meta: {
      title: '机构风险信息列表'
    }
  },
  {
    path: '/checkRight',
    name: 'checkRight',
    component: () => import('@/views/checkRight'),
    meta: {
      title: '权限'
    }
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面meta */
  // if (to.meta.content) {
  //   let head = document.getElementsByTagName('head')
  //   let meta = document.createElement('meta')
  //   meta.content = to.meta.content
  //   head[0].appendChild(meta)
  // }
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

export default router
